//动态路由存储数据,供项目使用
import {constantRoutes, asyncRoutes} from '@/router/index.js'
//导入自定义过滤路由的方法
import {filterAsyncRoutes} from '@/utils/routerUtil.js'

//1、私有变量，存放路由菜单
const state = () => ({
  routes: [],
});


//2、私有变量对应的setter方法
const mutations = {
  setRoutes(state, value) {
    //所有的路由=常量路由 + 动态过滤的路由
    state.routes =  constantRoutes.concat(value);
  },
}

//3、 自定义的方法
const actions = {
   // permissions参数，传当前用户的权限
   loadRoutes({ commit }, permission){
      return new Promise((resolve, reject) => {
        const routerList=filterAsyncRoutes(asyncRoutes,permission);
         commit('setRoutes', routerList)

         resolve(routerList);
      })

   }

}

//暴露这个对象
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
